package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class DynamicSearchField implements PickableBean {

    public static final String FN_PATH = "path";
    public static final String FN_CALLNAME = "callName";
    public static final String FN_DISPLAYFIELD = "displayField";
    public static final String FN_REPORTFIELD = "reportField";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_PATH,
            FN_CALLNAME,
            FN_DISPLAYFIELD,
            FN_REPORTFIELD);

    private String path;
    private String callName;
    private String displayField;
    private String reportField;

    public DynamicSearchField(String path, String callName, String displayField, String reportField) {
        setPath(path);
        setCallName(callName);
        setDisplayField(displayField);
        setReportField(reportField);
    }

    public DynamicSearchField() {
    }

    public DynamicSearchField duplicate() {
        DynamicSearchField result = new DynamicSearchField();
        result.path = this.path;
        result.callName = this.callName;
        result.displayField = this.displayField;
        result.reportField = this.reportField;
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String value) {
        this.path = value;
    }

    public DynamicSearchField withPath(String value) {
        setPath(value);
        return this;
    }

    public DynamicSearchField withPath(String value, String defaultValue) {
        setPath(value == null ? defaultValue : value);
        return this;
    }

    public String getCallName() {
        return callName;
    }

    public void setCallName(String value) {
        this.callName = value;
    }

    public DynamicSearchField withCallName(String value) {
        setCallName(value);
        return this;
    }

    public DynamicSearchField withCallName(String value, String defaultValue) {
        setCallName(value == null ? defaultValue : value);
        return this;
    }

    public String getDisplayField() {
        return displayField;
    }

    public void setDisplayField(String value) {
        this.displayField = value;
    }

    public DynamicSearchField withDisplayField(String value) {
        setDisplayField(value);
        return this;
    }

    public DynamicSearchField withDisplayField(String value, String defaultValue) {
        setDisplayField(value == null ? defaultValue : value);
        return this;
    }

    public String getReportField() {
        return reportField;
    }

    public void setReportField(String value) {
        this.reportField = value;
    }

    public DynamicSearchField withReportField(String value) {
        setReportField(value);
        return this;
    }

    public DynamicSearchField withReportField(String value, String defaultValue) {
        setReportField(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "path":
                return getPath();
            case "callName":
                return getCallName();
            case "displayField":
                return getDisplayField();
            case "reportField":
                return getReportField();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof DynamicSearchField)) return false;
        return signature().equals(((DynamicSearchField) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean DynamicSearchField has no signature defined, no hash code or equals possible.");
    }
}
